<?php
namespace app\api\service\provider;
/**
 * UserProvider
 * @Author Vee7
 * @Date 2021/12/23
 * @Time 15:01
 * @Description Vee7
 */

use app\api\service\serv\ArticleService;
use app\api\model\Article;
use think\Model;


class ArticleProvider implements ArticleService
{
    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取文章分页列表
     */
    public function getArticleListPage($page,$limit,$orderby='id',$order='asc'):array
    {
        return Article::with(['admin_root_group','admin_root'])->limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()
            ->toArray();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取文章分页列表
     */
    public function searchArticleListPage($field,$where,$searchData,$page,$limit,$orderby='id',$order='asc'):array
    {
        return Article::with(['admin_root_group','admin_root'])->where($field,$where,$searchData)->limit((($page-1)
            *$limit),
            $limit)->order($orderby, $order)
            ->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取文章总数据量
     */
    public function getArticleListCount():int
    {
        return Article::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取文章列表
     */
    public function getArticleList($orderby='id',$order='asc'):array
    {
        return Article::order($orderby, $order)->select()->toArray();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 10:22
     * @Description 获取指定id的文章数据
     */
    public function getArticleData($id):array
    {
        $return = Article::find($id);
        if(!$return){
            $return = ['code' => 400,'message' => '未找到文章'];
            return $return;
        }
        return $return->toArray();
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除文章
     */
    public function delArticleList(array $id):bool
    {
        $list = Article::select($id);
        $result = $list->delete();
        return $result;
    }

    /***
     * @param array $data
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:43
     * @Description 根据指定参数批量修改文章字段值
     *
     */
    public function editArticleList(array $data)
    {
        //前端应传递如下格式的二维数组
        //var editData = [{'id':id,'key':key,'value':value},{...}];
        $Article_obj = new Article;
        foreach ($data as $k=>$v){
            foreach ($v as $key=>$value){
                $list[] = $value;
            }
        }
        //返回修改后的数据
        return $Article_obj->saveAll($list)->toArray();
    }

    /***
     * @param array $addArray
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增文章
     */
    public function addArticleList(array $addArray,$author,$type = 1)
    {
        $Article_obj = new Article;
        $list = [];
        foreach ($addArray as $v){
            //saveAll函数无法自动添加时间戳，手动添加
            $v['create_time'] = date("Y-m-d H:i:s ");
            $v['status'] = 1;
            $v['author_id'] = $author['id'];
            $v['author_type'] = $type;
            $list[] = $v;
        }
        return $Article_obj->saveAll($list)->toArray();
    }

}